/*-------------------------------------------------------------------------------------------------------------------------
	Display your money
-------------------------------------------------------------------------------------------------------------------------*/

local PLUGIN = {}
PLUGIN.Title = "Balance"
PLUGIN.Description = "Check your balance!"
PLUGIN.Author = "Overv/Matt J"
PLUGIN.ChatCommand = "balance"
PLUGIN.Usage = ""
PLUGIN.Privileges = { "Balance" }

function PLUGIN:Call( ply, args )
	if ( ply:EV_HasPrivilege( "Balance" ) ) then
			evolve:Notify( ply, evolve.colors.white, "Please wait, Retrieving your balance.")
			YourBalance = tostring(ply:GetProperty( "Money" ))
			timer.Simple(2, function()
			evolve:Notify( ply, evolve.colors.white, "Your balance is: ", evolve.colors.red, YourBalance )
			end)
	else
		evolve:Notify( ply, evolve.colors.red, evolve.constants.notallowed )
	end
end

evolve:RegisterPlugin( PLUGIN )

/*-------------------------------------------------------------------------------------------------------------------------
	Buy things!
-------------------------------------------------------------------------------------------------------------------------*/

local PLUGIN = {}
PLUGIN.Title = "Buy"
PLUGIN.Description = "Buy things!"
PLUGIN.Author = "Overv/Matt J"
PLUGIN.ChatCommand = "buy"
PLUGIN.Usage = "<item>"
PLUGIN.Privileges = { "Buy" }

function PLUGIN:Call( ply, args )
	if ( ply:EV_HasPrivilege( "Buy" ) ) then
	if args[1] == "sent_mattcore" then Name = "Matt Core" Item = "sent_mattcore" Price = 100 elseif
	args[1] == "sent_nyancore" then Name = "Nyan Core" Item = "sent_nyancore" Price = 200 elseif
	args[1] == "sent_bbdcore" then Name = "BBD Core" Item = "sent_bbdcore" Price = 100 elseif
	args[1] == "weapon_ak47" then Name = "AK47" Item = "weapon_ak47" Price = 350 elseif
	args[1] == "weapon_deagle" then Name = "Deagle" Item = "weapon_deagle" Price = 150 elseif
	args[1] == "weapon_glock" then Name = "Glock" Item = "weapon_glock" Price = 100 elseif
	args[1] == "weapon_m4" then Name = "M16" Item = "weapon_m4" Price = 300 elseif
	args[1] == "weapon_mac10" then Name = "Mac 10" Item = "weapon_mac10" Price = 250 elseif
	args[1] == "weapon_mp5" then Name = "MP5" Item = "weapon_mp5" Price = 350 elseif
	args[1] == "weapon_para" then Name = "Para" Item = "weapon_para" Price = 425 elseif
	args[1] == "weapon_pumpshotgun" then Name = "Pump Shotgun" Item = "weapon_pumpshotgun" Price = 325 elseif
	args[1] == "weapon_tmp" then Name = "TMP" Item = "weapon_tmp" Price = 275 elseif
	args[1] == "sent_tourcore" then Name = "Tourettes Core" Item = "sent_tourcore" Price = 150 else
	evolve:Notify( ply, evolve.colors.red, "ERROR: Invalid choice!")
	return end
			A = tostring(ply:GetProperty( "Money" ))
			B = Price
			evolve:Notify( ply, evolve.colors.white, "Please wait.." )
			timer.Simple(2, function()
			C = A - B
			if C > -1 then
				ply:SetProperty( "Money", C )
				evolve:CommitProperties()
				else
				evolve:Notify( ply, evolve.colors.red, "ERROR: You do not have enough money to complete this transaction!" ) return end
			evolve:Notify( ply, evolve.colors.white, "Thanks for buying: ", evolve.colors.red, Name )
	
	local tr = ply:GetEyeTraceNoCursor()
		
		if ( !tr.Hit ) then return end
	
	local SpawnPos = tr.HitPos + tr.HitNormal * 16
	
	local boughtent = ents.Create( Item )
		boughtent:SetPos( SpawnPos )
		boughtent:Spawn()
		boughtent:Activate()
	
	undo.Create( Name )
	undo.AddEntity( boughtsent ) 
	undo.SetPlayer( Ply ) 
	undo.Finish()
	
	return ent

	end)
	else
		evolve:Notify( ply, evolve.colors.red, evolve.constants.notallowed )
	end
end

evolve:RegisterPlugin( PLUGIN )

/*-------------------------------------------------------------------------------------------------------------------------
	See info about things!
-------------------------------------------------------------------------------------------------------------------------*/

local PLUGIN = {}
PLUGIN.Title = "Info"
PLUGIN.Description = "See info about things!"
PLUGIN.Author = "Overv/Matt J"
PLUGIN.ChatCommand = "info"
PLUGIN.Usage = "<item>"
PLUGIN.Privileges = { "Info" }

function PLUGIN:Call( ply, args )
	if ( ply:EV_HasPrivilege( "Info" ) ) then
	if args[1] == "sent_mattcore" then Name = "Matt Core" Item = "sent_mattcore" Price = 100 elseif
	args[1] == "sent_nyancore" then Name = "Nyan Core" Item = "sent_nyancore" Price = 200 elseif
	args[1] == "sent_bbdcore" then Name = "BBD Core" Item = "sent_bbdcore" Price = 100 elseif
	args[1] == "weapon_ak47" then Name = "AK47" Item = "weapon_ak47" Price = 350 elseif
	args[1] == "weapon_deagle" then Name = "Deagle" Item = "weapon_deagle" Price = 150 elseif
	args[1] == "weapon_glock" then Name = "Glock" Item = "weapon_glock" Price = 100 elseif
	args[1] == "weapon_m4" then Name = "M16" Item = "weapon_m4" Price = 300 elseif
	args[1] == "weapon_mac10" then Name = "Mac 10" Item = "weapon_mac10" Price = 250 elseif
	args[1] == "weapon_mp5" then Name = "MP5" Item = "weapon_mp5" Price = 350 elseif
	args[1] == "weapon_para" then Name = "Para" Item = "weapon_para" Price = 425 elseif
	args[1] == "weapon_pumpshotgun" then Name = "Pump Shotgun" Item = "weapon_pumpshotgun" Price = 325 elseif
	args[1] == "weapon_tmp" then Name = "TMP" Item = "weapon_tmp" Price = 275 elseif
	args[1] == "sent_tourcore" then Name = "Tourettes Core" Item = "sent_tourcore" Price = 150 else
	evolve:Notify( ply, evolve.colors.red, "ERROR: Invalid choice!")
	return end
			A = tostring(ply:GetProperty( "Money" ))
			B = Price
			evolve:Notify( ply, evolve.colors.white, "Please wait.." )
			timer.Simple(2, function()
			C = A - B
			C2 = tostring(C)
			Price2 = tostring(Price)
			evolve:Notify( ply, evolve.colors.white, "Name: ", evolve.colors.red, Name )
			evolve:Notify( ply, evolve.colors.white, "Price: ", evolve.colors.red, Price2 )
			evolve:Notify( ply, evolve.colors.white, "Current Balance: ", evolve.colors.red, A )
			evolve:Notify( ply, evolve.colors.white, "Balance after purchase: ", evolve.colors.red, C2 )
	end)
	else
		evolve:Notify( ply, evolve.colors.red, evolve.constants.notallowed )
	end
end

evolve:RegisterPlugin( PLUGIN )

/*-------------------------------------------------------------------------------------------------------------------------
	Set your money!
-------------------------------------------------------------------------------------------------------------------------*/

local PLUGIN = {}
PLUGIN.Title = "Set Money"
PLUGIN.Description = "Set your money!"
PLUGIN.Author = "Overv/Matt J"
PLUGIN.ChatCommand = "setmoney"
PLUGIN.Usage = "<money>"
PLUGIN.Privileges = { "Set Money" }

function PLUGIN:Call( ply, args )
    if ( ply:EV_HasPrivilege( "Set Money" ) ) then
    if ( #args > 0 ) then
            if not string.find(args[1], "[^%d]") then
                  ply:SetProperty( "Money", tonumber(args[1]) )
                  evolve:Notify( ply, evolve.colors.white, "You have set your balance to: ", evolve.colors.red, args[1] )
                  evolve:CommitProperties()
            else
                   evolve:Notify( ply, evolve.colors.red, "You cannot set your balance to a word!")
            end
        end
    else
        evolve:Notify( ply, evolve.colors.red, evolve.constants.notallowed )
    end
end

evolve:RegisterPlugin( PLUGIN )

/*-------------------------------------------------------------------------------------------------------------------------
	Give money away!
-------------------------------------------------------------------------------------------------------------------------*/

local PLUGIN = {}
PLUGIN.Title = "Give Money"
PLUGIN.Description = "Give your money away!"
PLUGIN.Author = "Overv/Matt J"
PLUGIN.ChatCommand = "givemoney"
PLUGIN.Usage = "<player> <money>"
PLUGIN.Privileges = { "Give Money", "Money Check" }

function PLUGIN:Call( ply, args )
	if ( ply:EV_HasPrivilege( "Give Money" ) ) then
	local players = evolve:FindPlayer( args, ply, true, true )
		if ( #players == 1 ) then
		if not string.find(args[2], "[^%d]") then
			A = tostring(players[1]:GetProperty( "Money" ))
			A2 = tostring(ply:GetProperty( "Money" ))
			B = tostring(args[2]) or 0
			evolve:Notify( ply, evolve.colors.white, "Please wait.." )
			timer.Simple(2, function()
			C = A + B
			D = A2 - B
			if D > -1 then
				players[1]:SetProperty( "Money", C )
				ply:SetProperty( "Money", D )
				evolve:Notify( ply, evolve.colors.white, "You have given ", evolve.colors.red, B, evolve.colors.white, " to: ", evolve.colors.red, players[1]:Nick() )
				evolve:Notify( players[1], evolve.colors.white, "You have received ", evolve.colors.red, B, evolve.colors.white, " from: ", evolve.colors.red, ply:Nick() )
				evolve:CommitProperties()
				else
				evolve:Notify( ply, evolve.colors.red, "ERROR: You do not have enough money to complete this transaction!" ) end
			end)
			else
                   evolve:Notify( ply, evolve.colors.red, "You cannot give people words as money!")
            end
		elseif ( #players > 1 ) then
				evolve:Notify( ply, evolve.colors.white, "Did you mean ", evolve.colors.red, evolve:CreatePlayerList( players, true ), evolve.colors.white, "?" )
			else
				evolve:Notify( ply, evolve.colors.red, evolve.constants.noplayers )
		end
	else
		evolve:Notify( ply, evolve.colors.red, evolve.constants.notallowed )
	end
end

/*-------------------------------------------------------------------------------------------------------------------------
	Automatic PlayTime functions!
-------------------------------------------------------------------------------------------------------------------------*/

function EV_MoneyCheck()
	for uid, data in pairs( evolve.PlayerInfo ) do
	ply = player.GetByUniqueID( data.UID )
	if ply and ply:IsValid() and ply:IsPlayer() then
	if ( !ply:GetProperty( "Money" ) ) then
		if data.PlayTime < 3600 then
			ply:SetProperty( "Money", 100 )
			evolve:Notify( data, evolve.colors.white, "You have been given 100 starter cash!" )
		else
			Money2 = 100 + math.floor(data.PlayTime / 60 / 60) * 50
		ply:SetProperty( "Money", Money2 )
			evolve:Notify( ply, evolve.colors.white, "Your money has been updated/set according to PlayTime!" )
		end
		end
		end
		evolve:CommitProperties()	
	end
end


function EV_ForceMoneyCheckAll()
	for uid, data in pairs( evolve.PlayerInfo ) do
	ply = player.GetByUniqueID( data.UID )
	if ply and ply:IsValid() and ply:IsPlayer() then
		if data.PlayTime < 3600 then
			ply:SetProperty( "Money", 100 )
			evolve:Notify( data, evolve.colors.white, "You have been given 100 starter cash!" )
		else
			Money2 = 100 + math.floor(data.PlayTime / 60 / 60) * 50
		ply:SetProperty( "Money", Money2 )
			evolve:Notify( ply, evolve.colors.white, "Your money has been updated/set according to PlayTime!" )
		end
		end
		evolve:CommitProperties()	
	end
end


function EV_ForceMoneyCheck(player, command, args)
local players = evolve:FindPlayer( args, ply, true, true )
		if ( #players == 1 ) then
		print(players[1]:Nick())
		print(players[1]:UniqueID())
		UID = players[1]:UniqueID()
	ply = player.GetByUniqueID( UID )
	if ply and ply:IsValid() and ply:IsPlayer() then
		if data.PlayTime < 3600 then
			ply:SetProperty( "Money", 100 )
			evolve:Notify( data, evolve.colors.white, "You have been given 100 starter cash!" )
		else
			Money2 = 100 + math.floor(data.PlayTime / 60 / 60) * 50
		ply:SetProperty( "Money", Money2 )
			evolve:Notify( ply, evolve.colors.white, "Your money has been updated/set according to PlayTime!" )
		end
		end
		end
		evolve:CommitProperties()	
end

function EV_EmergencyReset()
for uid,info in pairs(evolve.PlayerInfo) do if ( info.UID ) then info.UID = nil end end
timer.Simple(5,function()
for uid,info in pairs(evolve.PlayerInfo) do if ( info.Money ) then info.Money = nil end end
timer.Simple(5,function()
EV_ForceMoneyCheckAll()
end)
end)
end

function PLUGIN:MoneyActivate( ply )
	local first = !ply:GetProperty( "LastJoin" )
	
	if ( first ) then
		ply:SetProperty( "UID", ply:UniqueID() )
		ply:SetProperty( "Money", 100 )

		evolve:CommitProperties()
	else
		ply:SetProperty( "UID", ply:UniqueID() )
		if ( !ply:GetProperty( "Money" ) ) then ply:SetProperty( "Money", 100 ) end
end
end

function PLUGIN:PlayerInitialSpawn( ply )
	ply.EV_MoneyInitialize = true
end

function PLUGIN:PlayerSpawn( ply )
	if ( ply.EV_MoneyInitialize ) then
		self:MoneyActivate( ply )
		ply.EV_MoneyInitialize = false
	end
end

timer.Create( "EV_UpdateMoney", 3600, 0, function()
for _, ply in ipairs( player.GetAll() ) do
		local Balance = ply:GetProperty ( "Money" )
		timer.Simple(2,function()
		local Balance2 = Balance + 50
		evolve:Notify( ply, evolve.colors.white, "Balance increased by 50!" )
		ply:SetProperty( "Money", Balance2 )
		end )
	end
end )

concommand.Add( "ev_forcemoneycheck", EV_ForceMoneyCheck )
//concommand.Add( "ev_forcemoneycheckall", EV_ForceMoneyCheckAll )
concommand.Add( "ev_moneycheck", EV_MoneyCheck )

evolve:RegisterPlugin( PLUGIN )